import { ref, reactive } from 'vue'
import { getRandomValue, againGroup } from '../../composables/util'



import { infoSelbj, infoSelgpdxt, infoSelinfo, infoSelsy, infoSeltfktxt, infoSeltjpsxt, sxtSelinfo, infoselxfgjxx, sxtselti } from '../../api/home.js'
import { toast } from '../../composables/util.js'

export const fnaghumen = ref([
  { status: 1, name: '总控室防爆门' },
  { status: 2, name: '发电机房防护门' },
  { status: 1, name: '总控室防爆门' },
  { status: 1, name: '空调机房门' },
])

export const getOneList = () => {
  let P1 = ['作战指挥室', '电站控制室', '发电机房', '总控室', '空调机房', '水库泵房', '污水泵房',]
  let P2 = [
    { name: '待定', value: '温湿度传感器', value1: '总控室' },
    { name: '待定', value: '门柴油发电机', value1: '发电机房' },
    { name: '待定', value: '温湿度传感器', value1: '作战指挥室' },
  ]
  let temporaryTableList = []
  for (let index = 0; index < 12; index++) {
    let O = getRandomValue(P2)
    temporaryTableList.push([
      index,
      O.name,
      O.value,
      getRandomValue(P1)
    ])
  }

}











/*
首页
left1
*/

export const Loading = ref(true)

export const left1Data = reactive({
  csgd: '',  //城市供电
  cyfdj: '', //柴油发电机
  dqtffs: ''//当前通风方式
})
export const leftOneConfig = ref({
  header: ['序号', '传感器位置', '温度(°C)', '湿度(%)'],
  data: [],
  rowNum: 10,
  headerBGC: 'rgba(7, 38, 73,0.9)',
  oddRowBGC: 'rgba(6, 30, 61,0.7)',
  evenRowBGC: 'rgba(8, 14, 39,0.7)',
  headerHeight: 32,
  carousel: 'single',
  waitTime: 2000,
  columnWidth: [68, 120, 90, 90]
})

// right1
export const shuiku = ref([
  { status: '', name: '饮用水箱', height: '' },
  { status: '', name: '生活水箱', height: '' },
  { status: '', name: '电站水箱', height: '' },
  { status: '', name: '污水井', height: '' },
])

export const protectiveDoor = ref([])
export const rightOneConfig = ref({
  header: ['序号', '传感器类型', '传感器位置', '数值'],
  data: [],
  rowNum: 5,
  headerBGC: 'rgba(7, 38, 73,0.9)',
  oddRowBGC: 'rgba(6, 30, 61,0.7)',
  evenRowBGC: 'rgba(8, 14, 39,0.7)',
  headerHeight: 32,
  carousel: 'single',
  waitTime: 2000,
  columnWidth: [58, 100, 120, 90]
})

// 首页其他
// 三状态（全部、传感器、漫游）
export const type1 = ref(false)
export const type2 = ref(false)
export const type3 = ref(false)
export const setType = (num) => {
  let Q
  if (num === 1) {
    Q = type1.value ? '14' : '13'
    type1.value = !type1.value
  }
  if (num === 2) {
    Q = type2.value ? '16' : '15'

    type2.value = !type2.value

  }
  if (num === 3) {
    Q = '17'
  }
  console.log(Q)
  ue4("QuestPosition", {
    "PositionName": Q
  })
}


export const sirenConfig = ref({
  header: ['编号', '告警区域', '告警内容', '定位'],
  data: [],
  rowNum: 10,
  headerBGC: 'rgba(71, 27, 29,0.9)',
  oddRowBGC: 'rgba(38, 9, 11,0.7)',
  evenRowBGC: 'rgba(57, 26, 26,0.7)',
  headerHeight: 32,
  carousel: 'single',
  waitTime: 2000,
  columnWidth: [60, 120, 140, 80]
})


// left2
export const videoList = ref([])
export const videoArr = ref([])


export const activerVideo = ref(null)
export const openVide = ref(false)

// left3
export const left3Data = ref(null)
export const leftThreeConfig = ref({
  header: ['序号', '阀门编号', '阀门状态', '阀门位置'],
  data: [],
  rowNum: 6,
  headerBGC: 'rgba(7, 38, 73,0.9)',
  oddRowBGC: 'rgba(6, 30, 61,0.7)',
  evenRowBGC: 'rgba(8, 14, 39,0.7)',
  headerHeight: 32,
  carousel: 'single',
  waitTime: 2000,
  columnWidth: [68, 90, 90, 120]
})

export const exhaustFan = ref([
  // {
  //   type: 0,  // 0未启动 1启动(blue_bg1) 2故障(red_bg1)
  //   name: '排风机',
  //   className: 'grey_bg1',
  //   data: [
  //     { name: '风机位置：', value: '北-一口排风机房' },
  //   ]
  // },
  // {
  //   type: 1,  // 0未启动 1启动 2故障
  //   className: 'blue_bg1',
  //   name: '电站进风机',
  //   data: [
  //     { name: '风机位置：', value: '电站控制室' },
  //   ]
  // },
  // {
  //   type: 2,  // 0未启动 1启动 2故障
  //   className: 'red_bg1',
  //   name: '电站排风机',
  //   data: [
  //     { name: '风机位置：', value: '电站控制室' },
  //   ]
  // },

  // {
  //   type: 1,  // 0未启动 1启动 2故障
  //   className: 'blue_bg1',
  //   name: '排风机',
  //   data: [
  //     { name: '风机位置：', value: '南-一口排风机房' },
  //   ]
  // },


  // // 下层
  // {
  //   type: 1,  // 0未启动 1启动 2故障
  //   className: 'blue_bg2',
  //   name: '滤毒风机',
  //   data: [
  //     { name: '风机位置：', value: '北-二口进风机房' },
  //     { name: '风机模式：', value: '手动模式' },
  //   ]
  // },

  // {
  //   type: 2,  // 0未启动 1启动 2故障
  //   className: 'red_bg2',
  //   name: '清洁风机',
  //   data: [
  //     { name: '风机位置：', value: '北-二口进风机房' },
  //     { name: '风机模式：', value: '手动模式' },
  //   ]
  // },
  // {
  //   type: 0,  // 0未启动 1启动 2故障
  //   className: 'grey_bg2',
  //   name: '清洁风机',
  //   data: [
  //     { name: '风机位置：', value: '南-三口进风机房' },
  //     { name: '风机模式：', value: '自动模式' },
  //   ]
  // },
  // {
  //   type: 1,  // 0未启动 1启动 2故障
  //   className: 'blue_bg2',
  //   name: '滤毒风机',
  //   data: [
  //     { name: '风机位置：', value: '南-三口进风机房' },
  //     { name: '风机模式：', value: '自动模式' },
  //   ]
  // },

])


// left4
export const left4Data = ref(null)
export const left4List = ref([])

// left5
export const left5Data = ref(null)
export const genset1 = ref(null)
export const genset2 = ref(null)
// right5
export const distributionCabinet = ref(null)

// left6
export const left6Data = ref(null)
export const leftsixConfig = ref({
  header: ['设备id', '设备地址', '设备状态'],
  data: [],
  rowNum: 22,
  headerBGC: 'rgba(7, 38, 73,0.9)',
  oddRowBGC: 'rgba(6, 30, 61,0.7)',
  evenRowBGC: 'rgba(8, 14, 39,0.7)',
  headerHeight: 32,
  carousel: 'single',
  waitTime: 2000,
  columnWidth: [100, 168, 100]
})

export const rightsixConfig = ref({
  header: ['设备id', '设备地址', '设备状态'],
  data: [],
  rowNum: 26,
  headerBGC: 'rgba(7, 38, 73,0.9)',
  oddRowBGC: 'rgba(6, 30, 61,0.7)',
  evenRowBGC: 'rgba(8, 14, 39,0.7)',
  headerHeight: 32,
  carousel: 'single',
  waitTime: 2000,
  columnWidth: [100, 168, 100]

})


export function randomString(length) {
  const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  let result = ''
  for (let i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]
  return result
}

export const compentsKey = ref(randomString(5))



// 告警
export const getError = () => {
  infoSelbj().then(res => {
    if (res.data.result) {
      sirenConfig.value.data = res.data.result.map(v => [v.id, v.gjqy, v.bjname, '<span class="underline cursor-pointer" >定位</span>',])
    }
  })
}


const isInterval = ref(false)

// 总函数
export const getAppInfo = () => {
  Loading.value = !isInterval.value





  // 前端首页数据查询
  // 摄像头列表查询   
  // 前端通风空调系统
  // 前端给排水系统
  // 供配电系统
  // UE数据查询
  // 消防告警信息

  Promise.all([infoSelsy(), sxtSelinfo(), infoSeltfktxt(), infoSeltjpsxt(), infoSelgpdxt(), infoSelinfo(), infoselxfgjxx()])
    .then((res) => {
      console.log(res)
      // left1
      let temporary0 = res[0].data.result
      Object.keys(left1Data).forEach((key) => left1Data[key] = temporary0[key])
      leftOneConfig.value.data = temporary0.wsds.map((v, i) => [i, v.cgqwz, v.wd, v.sd])

      // right1
      shuiku.value = [
        { status: temporary0.yinysxswzt, name: '饮用水箱', height: temporary0.yinysxswgd },
        { status: temporary0.shsxswzt, name: '生活水箱', height: temporary0.shsxswgd },
        { status: temporary0.dzsxswzt, name: '电站水箱', height: temporary0.dzsxswgd },
        { status: temporary0.wsxswzt, name: '污水井', height: temporary0.wsxswgd },
      ]
      protectiveDoor.value = [
        {
          type: temporary0.rfm1zt == '防护门关' ? 1 : 2,
          name: '人防门 1',
          className: temporary0.rfm2zt == '防护门关' ? 'blue_bg1' : 'red_bg1',
          data: [
            { name: '人防门模式：', value: temporary0.rfm1ms },
          ]
        },
        {
          type: temporary0.rfm2zt == '防护门关' ? 1 : 2,
          className: temporary0.rfm2zt == '防护门关' ? 'blue_bg1' : 'red_bg1',
          name: '人防门 2',
          data: [
            { name: '人防门模式：', value: temporary0.rfm2ms },
          ]
        },
        {
          type: temporary0.rfm3zt == '防护门关' ? 1 : 2,
          className: temporary0.rfm3zt == '防护门关' ? 'blue_bg1' : 'red_bg1',
          name: '人防门 3',
          data: [
            { name: '人防门模式：', value: temporary0.rfm3ms },
          ]
        },
      ]
      rightOneConfig.value.data = temporary0.qt.map((v, i) => [i, v.cgqlx, v.cgqwz, v.sz])


      // 2
      let arrVideo = res[1].data.result.map(v => {
        return {
          ...v,
          status: true
        }
      })
      videoArr.value = arrVideo
      videoList.value = againGroup(arrVideo, 4)






      // left3
      let temporary2 = res[2].data.result

      left3Data.value = temporary2
      leftThreeConfig.value.data = temporary2.fmResponseList.map((v, i) => [i, v.fmbh, v.fmzt == '故障' ? `<span style="color:#ff8080;">故障</span>` : v.fmzt, v.fmwz])
      exhaustFan.value = temporary2.jfjResponseList.map(v => {
        let type = v.zt == '启动' ? 1 : v.zt == '故障' ? 2 : 0  // 0未启动 1启动(blue_bg1) 2故障(red_bg1)
        let data = [{ name: '风机位置：', value: v.wz }]
        if (v.ms) {
          data.unshift({ name: '风机模式：', value: v.ms })
        }
        let className = (type == 0 ? `grey_bg` : type == 1 ? 'blue_bg' : 'red_bg') + data.length
        return {
          type,
          name: v.name,
          className,
          data
        }
      })



      // left4
      left4Data.value = res[3].data.result
      left4List.value = [
        { type: left4Data.value.wsb1zt == '启动' ? 1 : 2, box: 'box1', name: '污水泵1', },
        { type: left4Data.value.wsb2zt == '启动' ? 1 : 2, box: 'box', name: '污水泵2', },
        { type: left4Data.value.sb1zt == '启动' ? 1 : 2, box: 'box1', name: '水泵1', },
        { type: left4Data.value.sb2zt == '启动' ? 1 : 2, box: 'box', name: '水泵2', },
      ]
      // left5
      left5Data.value = res[4].data.result
      distributionCabinet.value = res[4].data.result.pdg.map(v => {
        return {
          name: v.name,
          status: v.dl,
          fuelOil: '电流',
          percent: v.dliang,
          data: [
            { name: '低压回路1状态：', value: v.dyhl1zt, w: 'min-w-[92px]' },
            { name: '低压回路7状态：', value: v.dyhl7zt, w: 'min-w-[97px]' },
            { name: '低压回路2状态：', value: v.dyhl2zt, w: 'min-w-[92px]' },
            { name: '低压回路8状态：', value: v.dyhl8zt, w: 'min-w-[97px]' },
            { name: '低压回路3状态：', value: v.dyhl3zt, w: 'min-w-[92px]' },
            { name: '低压回路9状态：', value: v.dyhl9zt, w: 'min-w-[97px]' },
            { name: '低压回路4状态：', value: v.dyhl4zt, w: 'min-w-[92px]' },
            { name: '低压回路10状态：', value: v.dyhl10zt, w: 'min-w-[97px]' },

            { name: '低压回路5状态：', value: v.dyhl5zt, w: 'min-w-[92px]' },
            { name: '低压回路11状态：', value: v.dyhl11zt, w: 'min-w-[97px]' },

            { name: '低压回路6状态：', value: v.dyhl6zt, w: 'min-w-[92px]' },
            { name: '低压回路12状态：', value: v.dyhl12zt, w: 'min-w-[97px]' },
          ]
        }
      })


      console.log(distributionCabinet.value)

      // left6
      left6Data.value = res[6].data.result

      let sixOne = []
      let sixTwo = []

      left6Data.value.forEach((v) => {
        if (v.hl == '1') {
          sixOne.push([v.id, v.wz, v.zt == '1' ? `<span style="color:#ff8080;">火警</span>` : v.zt == '2' ? '<span style="color:#e5e510;">故障</span>' : '正常'])
        } else {
          sixTwo.push([v.id, v.wz, v.zt == '1' ? `<span style="color:#ff8080;">火警</span>` : v.zt == '2' ? '<span style="color:#e5e510;">故障</span>' : '正常'])
        }
      })
      leftsixConfig.value.data = sixOne
      rightsixConfig.value.data = sixTwo





    })
    .catch((err) => {
      Loading.value = true
    }).finally(() => {
      Loading.value = false
      isInterval.value = true
    })
}



export const getData1 = () => {
  infoSelsy().then(res => {
    let temporary0 = res.data.result
    Object.keys(left1Data).forEach((key) => left1Data[key] = temporary0[key])
    let T = temporary0.wsds.map((v, i) => [i, v.cgqwz, v.wd, v.sd])
    // 
    console.log(JSON.stringify(leftOneConfig.value.data) == JSON.stringify(T))
    if (JSON.stringify(leftOneConfig.value.data) !== JSON.stringify(T)) {
      leftOneConfig.value.data = T
    }
    // right1
    shuiku.value = [
      { status: temporary0.yinysxswzt, name: '饮用水箱', height: temporary0.yinysxswgd },
      { status: temporary0.shsxswzt, name: '生活水箱', height: temporary0.shsxswgd },
      { status: temporary0.dzsxswzt, name: '电站水箱', height: temporary0.dzsxswgd },
      { status: temporary0.wsxswzt, name: '污水井', height: temporary0.wsxswgd },
    ]
    protectiveDoor.value = [
      {
        type: temporary0.rfm1zt == '防护门关' ? 1 : 2,
        name: '人防门 1',
        className: temporary0.rfm2zt == '防护门关' ? 'blue_bg1' : 'red_bg1',
        data: [
          { name: '人防门模式：', value: temporary0.rfm1ms },
        ]
      },
      {
        type: temporary0.rfm2zt == '防护门关' ? 1 : 2,
        className: temporary0.rfm2zt == '防护门关' ? 'blue_bg1' : 'red_bg1',
        name: '人防门 2',
        data: [
          { name: '人防门模式：', value: temporary0.rfm2ms },
        ]
      },
      {
        type: temporary0.rfm3zt == '防护门关' ? 1 : 2,
        className: temporary0.rfm3zt == '防护门关' ? 'blue_bg1' : 'red_bg1',
        name: '人防门 3',
        data: [
          { name: '人防门模式：', value: temporary0.rfm3ms },
        ]
      },
    ]
    let B = temporary0.qt.map((v, i) => [i, v.cgqlx, v.cgqwz, v.sz])
    if (JSON.stringify(rightOneConfig.value.data) !== JSON.stringify(B)) {
      rightOneConfig.value.data = B
    }
  })
}

export const getData3 = () => {
  infoSeltfktxt().then(res => {
    let temporary2 = res.data.result

    left3Data.value = temporary2
    let C = temporary2.fmResponseList.map((v, i) => [i, v.fmbh, v.fmzt == '故障' ? `<span style="color:#ff8080;">故障</span>` : v.fmzt, v.fmwz])
    if (JSON.stringify(leftThreeConfig.value.data) !== JSON.stringify(C)) {
      leftThreeConfig.value.data = C
    }



    exhaustFan.value = temporary2.jfjResponseList.map(v => {
      let type = v.zt == '启动' ? 1 : v.zt == '故障' ? 2 : 0  // 0未启动 1启动(blue_bg1) 2故障(red_bg1)
      let data = [{ name: '风机位置：', value: v.wz }]
      if (v.ms) {
        data.unshift({ name: '风机模式：', value: v.ms })
      }
      let className = (type == 0 ? `grey_bg` : type == 1 ? 'blue_bg' : 'red_bg') + data.length
      return {
        type,
        name: v.name,
        className,
        data
      }
    })



  })
}


export const getData4 = () => {
  infoSeltjpsxt().then(res => {
    // left4
    left4Data.value = res.data.result
    left4List.value = [
      { type: left4Data.value.wsb1zt == '启动' ? 1 : 2, box: 'box1', name: '污水泵1', },
      { type: left4Data.value.wsb2zt == '启动' ? 1 : 2, box: 'box', name: '污水泵2', },
      { type: left4Data.value.sb1zt == '启动' ? 1 : 2, box: 'box1', name: '水泵1', },
      { type: left4Data.value.sb2zt == '启动' ? 1 : 2, box: 'box', name: '水泵2', },
    ]
  })
}


export const getData5 = () => {
  infoSelgpdxt().then(res => {
    // left5
    left5Data.value = res.data.result
    distributionCabinet.value = res.data.result.pdg.map(v => {
      return {
        name: v.name,
        status: v.dl,
        fuelOil: '电流',
        percent: v.dliang,
        data: [
          { name: '低压回路1状态：', value: v.dyhl1zt, w: 'min-w-[92px]' },
          { name: '低压回路7状态：', value: v.dyhl7zt, w: 'min-w-[97px]' },
          { name: '低压回路2状态：', value: v.dyhl2zt, w: 'min-w-[92px]' },
          { name: '低压回路8状态：', value: v.dyhl8zt, w: 'min-w-[97px]' },
          { name: '低压回路3状态：', value: v.dyhl3zt, w: 'min-w-[92px]' },
          { name: '低压回路9状态：', value: v.dyhl9zt, w: 'min-w-[97px]' },
          { name: '低压回路4状态：', value: v.dyhl4zt, w: 'min-w-[92px]' },
          { name: '低压回路10状态：', value: v.dyhl10zt, w: 'min-w-[97px]' },

          { name: '低压回路5状态：', value: v.dyhl5zt, w: 'min-w-[92px]' },
          { name: '低压回路11状态：', value: v.dyhl11zt, w: 'min-w-[97px]' },

          { name: '低压回路6状态：', value: v.dyhl6zt, w: 'min-w-[92px]' },
          { name: '低压回路12状态：', value: v.dyhl12zt, w: 'min-w-[97px]' },
        ]
      }
    })
  })
}


export const getData6 = () => {
  infoselxfgjxx().then(res => {
    // left6
    left6Data.value = res.data.result

    let sixOne = []
    let sixTwo = []

    left6Data.value.forEach((v) => {
      if (v.hl == '1') {
        sixOne.push([v.id, v.wz, v.zt == '1' ? `<span style="color:#ff8080;">火警</span>` : v.zt == '2' ? '<span style="color:#e5e510;">故障</span>' : '正常'])
      } else {
        sixTwo.push([v.id, v.wz, v.zt == '1' ? `<span style="color:#ff8080;">火警</span>` : v.zt == '2' ? '<span style="color:#e5e510;">故障</span>' : '正常'])
      }
    })
    if (JSON.stringify(leftsixConfig.value.data) !== JSON.stringify(sixOne)) {
      console.log('变了')
      leftsixConfig.value.data = sixOne
    }
    if (JSON.stringify(rightsixConfig.value.data) !== JSON.stringify(sixTwo)) {
      console.log('变了1')
      rightsixConfig.value.data = sixTwo
    }
  })
}




